<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>评论列表</title>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
    <style>
        #content{
            width: 800px;
            margin: 0 auto;
            padding: 5px 10px;
            background-color:#eee;
        }
        .commentBox h1{
            background-color: #bbb;
        }
        .commentList{
            border: 1px solid yellow;
            padding:10px;
        }
        .commentList .comment{
            border: 1px solid #bbb;
            padding-left: 10px;
            margin-bottom:10px;
        }
        .commentList .commentAuthor{
            font-size: 20px;
        }
        .commentForm{
            margin-top: 20px;
            border: 1px solid red;
            padding:10px;
        }
        .commentForm textarea{
            width:100%;
            height:50px;
            margin:10px 0 10px 2px;
        }
    </style>
</head>
<body>
<div id="content"></div>
<script type="text/jsx">
    var staticData = [
        {author: "张飞", text: "我在写一条评论~！"},
        {author: "关羽", text: "2货，都知道你在写的是一条评论。。"},
        {author: "刘备", text: "哎，咋跟这俩逗逼结拜了！"}
    ];

    var converter = new Showdown.converter();//markdown
    var Comment = React.createClass({
        render: function (){
            var rawMarkup = converter.makeHtml(this.props.children.toString());
            return (
                    <div className="comment">
                        <h2 className="commentAuthor">
                            {this.props.author}:
                        </h2>
                        <span dangerouslySetInnerHTML={{__html: rawMarkup}} />
                    </div>
            );
        }
    });
    var CommentList = React.createClass({
        render: function (){
            var commentNodes = this.props.data.map(function (comment){
                return (
                        <Comment author={comment.author}>
                            {comment.text}
                        </Comment>
                );
            });

            return (
                    <div className="commentList">
                        {commentNodes}
                    </div>
            );
        }
    });
    var CommentForm = React.createClass({
        handleSubmit: function (e){
            e.preventDefault();
            var author = this.refs.author.getDOMNode().value.trim();
            var text = this.refs.text.getDOMNode().value.trim();
            if(!author || !text){
                return;
            }
            this.props.onCommentSubmit({author: author, text: text});
            this.refs.author.getDOMNode().value = '';
            this.refs.text.getDOMNode().value = '';
            return;
        },
        render: function (){
            return (
                    <form className="commentForm" onSubmit={this.handleSubmit}>
                        <input type="text" placeholder="Your name" ref="author" /><br/>
                        <textarea type="text" placeholder="Say something..." ref="text" ></textarea><br/>
                        <input type="submit" value="Post" />
                    </form>
            );
        }
    });
    var CommentBox = React.createClass({
        loadCommentsFromServer: function (){
            this.setState({data: staticData});
            /*
             方便起见，这里就不走服务端了，可以自己尝试
             $.ajax({
             url: this.props.url + "?_t=" + new Date().valueOf(),
             dataType: 'json',
             success: function (data){
             this.setState({data: data});
             }.bind(this),
             error: function (xhr, status, err){
             console.error(this.props.url, status, err.toString());
             }.bind(this)
             });
             */
        },
        handleCommentSubmit: function (comment){
            //TODO: submit to the server and refresh the list
            var comments = this.state.data;
            var newComments = comments.concat([comment]);

            //这里也不向后端提交了
            staticData = newComments;

            this.setState({data: newComments});
        },
        //初始化 相当于构造函数
        getInitialState: function (){
            return {data: []};
        },
        //组件添加的时候运行
        componentDidMount: function (){
            this.loadCommentsFromServer();
            this.interval = setInterval(this.loadCommentsFromServer, this.props.pollInterval);
        },
        //组件删除的时候运行
        componentWillUnmount: function() {
            clearInterval(this.interval);
        },
        //调用setState或者父级组件重新渲染不同的props时才会重新调用
        render: function (){
            return (
                    <div className="commentBox">
                        <h1>Comments</h1>
                        <CommentList data={this.state.data}/>
                        <CommentForm onCommentSubmit={this.handleCommentSubmit} />
                    </div>
            );
        }
    });

    //当前目录需要有comments.json文件
    //这里定义属性，如url、pollInterval，包含在props属性中
    React.render(
            <CommentBox url="comments.json" pollInterval="2000" />,
            document.getElementById("content")
    );
</script>

</body>
</html>